package com.ithuang.office.controller;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import static com.ithuang.office.utils.POIUtil.*;

/**
 * 常用word操作汇总
 * @author HJW
 */
@RestController
public class OfficeController {

    /**
     * 替换word当中的通配符 例如：${name}
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("/replaceWordText")
    public String replaceWordText(HttpServletResponse response) throws Exception {
        String filePath = "word通配符替换测试.docx";
        String outFilePath = "result.docx";
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("name","张三");
        dataMap.put("password","123456");
        InputStream is = new FileInputStream(filePath);
        XWPFDocument doc = new XWPFDocument(is);
        //替换段落里面的变量
        replaceInPara(doc, dataMap);
        //替换表格里面的变量
        replaceInTable(doc, dataMap);
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("result.docx", "UTF-8"));
        response.setContentType("multipart/form-data");
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        doc.write(os);
        close(os);
        close(is);
        return "Word通配符替换成功！";
    }

    /**
     * 替换word当中的书签
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/replaceWordTag")
    public String replaceWordTag(HttpServletResponse response) throws IOException {
        String filePath = "word书签替换测试.docx";
        String outFilePath = "result.docx";
        //读文件
        InputStream is = new FileInputStream(filePath);
        XWPFDocument document = new XWPFDocument(is);
        //书签数据
        Map<String, Object> bookTagMap = new HashMap<>();
        bookTagMap.put("name", "张三");
        bookTagMap.put("password", "123456");
        replaceBookTag(document,bookTagMap);
        //返回流
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + new String("result.docx".getBytes("utf-8"), "ISO-8859-1"));
        OutputStream outputStream = response.getOutputStream();
        document.write(outputStream);
        outputStream.flush();
        outputStream.close();
        document.close();
        is.close();
        return "Word书签替换成功！";
    }

    /**
     * word转换为pdf
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/wordToPdf")
    public String wordToPdf(HttpServletResponse response) throws IOException {
        String filePath = "word转换pdf测试.docx";
        String outFilePath = "result.pdf";
        InputStream in = Files.newInputStream(Paths.get(filePath));
        XWPFDocument document = new XWPFDocument(in);
        PdfOptions options = PdfOptions.create();
        OutputStream out = Files.newOutputStream(Paths.get(outFilePath));
        PdfConverter.getInstance().convert(document,out,options);
        return "pdf转换成功！";

    }

    /**
     * 模拟合同盖章
     * @return
     * @throws Exception
     */
    @GetMapping("/createWordSeal")
    public String createSeal() throws Exception {
        sealInWord("word盖章测试.docx","result.docx","公章.png","盖章",0,0,0,-30,false);
        return "盖章成功！";
    }

    /**
     * 通过word书签在段落中插入图片
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    @GetMapping("/replacePic")
    public String replacePic() throws IOException, InvalidFormatException {
        FileInputStream is = new FileInputStream("段落中插入图片测试.docx");
        XWPFDocument doc = new XWPFDocument(is);

        Map<String,InputStream> dataMap = new HashMap<>();
        dataMap.put("tag",new FileInputStream("公章.png"));
        dataMap.put("pic",new FileInputStream("公章.png"));
        replacePicByTag(doc,dataMap);
        FileOutputStream outputStream = new FileOutputStream("result.docx");
        doc.write(outputStream);
        outputStream.close();
        doc.close();
        is.close();
        return "插入成功！";
    }






}
